// 朝四个方向收缩
var spiralOrder = function (matrix) {
  const ans = []
  let left = 0, right = matrix[0].length
  let top = 0, bottom = matrix.length
  while(left < right && top < bottom) {
    for(let i = left; i < right; i++) {
      ans.push(matrix[top][i])
    }
    for(let i = top; i < bottom; i++) {
      ans.push(matrix[i][right])
    }
    for(let i = right; i > left; i--) {
      ans.push(matrix[bottom][i])
    }
    for(let i = bottom; i > top; i--) {
      ans.push(matrix[i][left])
    }
    top++
    left++
    bottom--
    right--
  }
  if(left == right) {
    for(let i = 0; i < matrix; i++) {
      ans.push(matrix[i][left])
    }
  }
  if(bottom == top) {
    for(let i = 0; i < n; i++) {
      ans.push(matrix[bottom][i])
    }
  }
  return ans
};